#! /bin/sh
### BEGIN INIT INFO
# Provides:          kvm
# Required-Start:    
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: KVM init script
# Description:       This script loads the kernel modules needed by kvm
### END INIT INFO

. /lib/lsb/init-functions

# Figure out which module we need.
if grep -q ^flags.*\\\<vmx\\\> /proc/cpuinfo
then
	module=kvm_intel
elif grep -q ^flags.*\\\<svm\\\> /proc/cpuinfo
then
	module=kvm_amd
else
	module=
fi

case "$1" in
  start)
	log_begin_msg "Loading kvm module $module"
	if [ -z "$module" ]
	then
		log_end_msg 1
		exit 0
	fi
	if modprobe "$module"
	then
		log_end_msg 0
	else
		log_end_msg 1
		exit 1
	fi
	;;
  stop)
	if [ -z "$module" ]
	then
		exit 0
	fi
	if lsmod | grep -q "$module"
	then
		log_begin_msg "Unloading kvm module $module"
		if rmmod "$module"
		then
			rmmod kvm
			log_end_msg 0
		else
			log_end_msg 1
			exit 1
		fi
	fi
	;;
  force-reload|restart)
	$0 stop
	$0 start
	;;
  *)
	echo "Usage: $0 {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac

:
